package cc.sheji.controller.admin;

import cc.sheji.Service.CoinLogService;
import cc.sheji.domain.CoinLog;
import cc.sheji.domain.CoinLogQueryCriteria;
import cc.sheji.domain.JsonResult;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@RestController

@RequestMapping(value = "/admin/v1/coinlog")
public class AdminCoinLogController {

    @Autowired
    CoinLogService coinLogService;

    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id) throws Exception {
        return JsonResult.Success(coinLogService.get(id));
    }

    @PostMapping("register")
    public JsonResult create(@RequestBody @Validated() CoinLog entity) {
        entity.setId(null);
        return JsonResult.Success(coinLogService.create(entity));
    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @Valid CoinLog entity) {
        entity.setId(id);
        return JsonResult.Success(coinLogService.update(entity));
    }


    @GetMapping()
    public JsonResult get(CoinLogQueryCriteria criteria , Pageable pageable) throws InterruptedException {
        return JsonResult.Success(coinLogService.list(criteria,pageable));
    }

    @GetMapping("/mine")
    public JsonResult getAll(CoinLogQueryCriteria criteria ) throws InterruptedException {
        criteria.setUserId(StpUtil.getLoginIdAsLong());
        return JsonResult.Success(coinLogService.getAll(criteria));
    }

}
